/*
 * $Id: RequestDataSet.java,v 1.3 2009/08/21 14:48:19 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.requestData;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.exceptions.BasicException;

/**
 * A request data set. This allows a data set to be opened.
 * 
 * @author 212042946
 * 
 */
public abstract class RequestDataSet {
	/**
	 * The name of the data set. This is the name used to refer to the data set
	 * by objects messaging an AscRequestData object.
	 */
	private String theName = null;

	/**
	 * The file which is the source of this data set. When opening this data
	 * set, this file will be opened. The stream returned from opening this file
	 * may be filtered before being made available to the requestor.
	 */
	private File theSourceFile = null;

	/**
	 * The file is a directory
	 */
	private boolean isDirectory = false;

	/**
	 * Construct a request data set.
	 * 
	 * @param name
	 *            The name of the data set. This is the name used to refer to
	 *            the data set by objects messaging an AscRequestData object.
	 * 
	 * @param sourceFile
	 *            The file which is the source of this data set. When opening
	 *            this data set, this file will be opened. The stream returned
	 *            from opening this file may be filtered before being made
	 *            available to the requestor.
	 */
	protected RequestDataSet(String name, File sourceFile) {
		theName = name;
		theSourceFile = sourceFile;
	}

	protected RequestDataSet(String name, File sourceFile, boolean directory) {
		theName = name;
		theSourceFile = sourceFile;
		isDirectory = directory;
	}

	/**
	 * Get the name of the data set. This is the name used to refer to the data
	 * set by objects messaging an AscRequestData object.
	 * 
	 * @return The name of the data set.
	 */
	public String getName() {
		return theName;
	}

	/**
	 * Get source file
	 * 
	 * @return
	 */
	public File getSourceFile() {
		return theSourceFile;
	}

	/**
	 * Get the pathname to the file which is the source of this data set.
	 * 
	 * @return The pathname to the file which is the source of this data set.
	 */
	public String getSourceFilePath() {
		return theSourceFile.getAbsolutePath();
	}

	/**
	 * Check if this file is a directory or not
	 * 
	 * @return
	 */
	public boolean isDirectory() {
		return isDirectory;
	}

	/**
	 * Open, for reading, the data set. The returned stream is buffered. It is
	 * the responsibility of the calling code to close the stream. At least one
	 * message will be logged indicating the opening of the data set.
	 * 
	 * @param myHelper
	 *            Helper object for reporting errors and logging success.
	 * 
	 * @return Buffered input stream to use for reading the data set.
	 * 
	 * @exception AscServiceAbortException
	 *                If unable to open the data set.
	 */
	public InputStream openDataSet() throws BasicException {
		InputStream dataSetStream = openStream();
		return dataSetStream;
	}

	/**
	 * Open, for reading, the data set. The returned stream is buffered. It is
	 * the responsibility of the calling code to close the stream. No messages
	 * need be logged on success, the caller will log the success message. This
	 * is a helper for the openDataSet() method.
	 * 
	 * @param myHelper
	 *            Helper object for reporting errors.
	 * 
	 * @return Buffered input stream to use for reading the data set.
	 * 
	 * @exception AscServiceAbortException
	 *                If unable to open the data set.
	 */
	protected abstract InputStream openStream() throws BasicException;

	/**
	 * Open, for reading, the source file as a raw file.
	 * 
	 * @param myHelper
	 *            Helper object for reporting errors.
	 * 
	 * @return Buffered input stream to use for reading the source file.
	 * 
	 * @exception AscServiceAbortException
	 *                If unable to open the source file.
	 */
	protected InputStream openSourceRaw(){
		String methodName = "openSourceRaw";
		InputStream rawSource = null;
		try {
			rawSource = new FileInputStream(theSourceFile);
		} catch (IOException e) {
			String message = "Unable to open " + theSourceFile + ": " + e;
			ASCLogger.error(this.getClass(), methodName, message);
		}
		return new BufferedInputStream(rawSource);
	}

	/**
	 * Open, for reading, the source file as a deflated input stream.
	 * 
	 * @param myHelper
	 *            Helper object for reporting errors.
	 * 
	 * @exception AscServiceAbortException
	 *                If unable to open the source file as a deflated input
	 *                stream.
	 */
	protected InputStream openSourceDeflated() {
		String methodName = "openSourceDeflated";
		InputStream bufferedInput = openSourceRaw();
		try {
			return new GZIPInputStream(bufferedInput);
		} catch (IOException e) {
			String message = "Unable to open gzipped file " + theSourceFile
					+ ": " + e;
			ASCLogger.error(this.getClass(), methodName, message);
			return null;
		}
	}

	/**
	 * Retrieve the size, in bytes, of the data set.
	 * 
	 * @param myHelper
	 *            Helper object for reporting errors.
	 * 
	 * @return The size, in bytes, of the data set.
	 * 
	 * @exception AscServiceAbortException
	 *                If unable to determine the size of the data set.
	 */
	public abstract long dataSetSize() throws BasicException;

	/**
	 * Return a string representation of this data set.
	 * 
	 * @return A string representation of the object.
	 */
	public String toString() {
		return new StringBuffer().append(getClass().getName()).append("(")
				.append(getName()).append(",").append(getSourceFilePath())
				.append(")").toString();
	}
}
